#! /usr/bin/env python
#-*- coding: UTF-8 -*-
import re

# Extract and backup the old ICON0.PNG
x = open("EBOOT.PBP","r").read(512000).encode("hex")
y = open("icon0_old.png","w")
r = re.compile("504e47(.*?)49454e44ae426082")
m = r.search(x)
icon = "89" + m.group()
y.write(icon.decode("hex"))
y.close()

# Replace ICON0.PNG with the new one
x = open("EBOOT.PBP","rb").read().encode("hex")
r = re.compile("504e47(.*?)49454e44ae426082")
m = r.search(x)
icon_old = "89" + m.group()
icon_new = open("icon0.png","rb").read().encode("hex")
x = x.replace(icon_old, icon_new)
open("EBOOT.PBP","wb").write(x.decode("hex"))
